/**
 * related to add.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-12
 */
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var global = deviceone.sm("do_Global");
var common = require("common");
var qqHttp = require("helper/http");
var userUtil = require("url/user");
var root = ui("$");
var param = {"id":""};

//andriod返回键
page.on("back", function() {
	page.fire("refreshAllData");
    app.closePage();
});


var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "编辑地址"
})
header.refreshData();

var layoutForm = ui("layout_form");
/*****门牌号******/
var addhouseNumber = layoutForm.add("houseNumber", "source://view/reuse/form/mutiInput.ui",0,0);
var houseNumber = ui("houseNumber");
houseNumber.bindData(hashdata);
hashdata.addData({
    "textTitle" : "地址补充",
    "hint":'门牌号、某个地点(如大门前)'
});
houseNumber.refreshData();
var do_TextField_houseNumber = ui(addhouseNumber+'.textInput');
/*****地区信息******/
var addCommunityAddr = layoutForm.add("communityAddr", "source://view/reuse/form/mutiLabel.ui",0,0);
var communityAddr = ui("communityAddr");
communityAddr.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "社区地址",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
communityAddr.refreshData();
communityAddr.on("touch","",300,function(){
	page.hideKeyboard();
	global.setMemory("communityAddrCount", 0);
	common.open("source://view/area/selectCommunityAddr.ui",{});
});
var text_communityAddr = ui(addCommunityAddr+'.labelText');

var addLine = layoutForm.add("line", "source://view/reuse/form/line.ui",0,0);
var line = ui("line");
line.refreshData();
/*****手机号码******/
var addaddrPhone = layoutForm.add("addrPhone", "source://view/reuse/form/mutiInput.ui",0,0);
var addrPhone = ui("addrPhone");
addrPhone.bindData(hashdata);
hashdata.addData({
    "textTitle" : "手机号码",
    "inputType" : "PHONENUMBER",
    "hint":'11位手机号码'
});
addrPhone.refreshData();
var do_TextField_phone = ui(addaddrPhone+'.textInput');
/*****收货人******/
var addaddrName = layoutForm.add("addrName", "source://view/reuse/form/mutiInput.ui",0,0);
var addrName = ui("addrName");
addrName.bindData(hashdata);
hashdata.addData({
    "textTitle" : "收货人",
    "inputType" : "ASC",
    "hint":'姓名'
});
addrName.refreshData();
var do_TextField_name = ui(addaddrName+'.textInput');
/*****确定按钮******/
var layoutBtn = ui("layout_btn");
var addbtnAddr = layoutBtn.add("btnAddr", "source://view/reuse/button.ui",0,0);
var btnAddr = ui("btnAddr");
btnAddr.bindData(hashdata);
hashdata.addData({
    "btnText" : "确定"
});
btnAddr.refreshData();

function getData(id) {
	qqHttp.post(userUtil.url.address.detail, {id:id}, function(data) {
		deviceone.print(JSON.stringify(data));
		var userAddress = data.userAddress;
		do_TextField_name.text = userAddress.contactName;
		do_TextField_phone.text = userAddress.contactPhone;
		text_communityAddr.text = userAddress.nextAddr;
		do_TextField_houseNumber.text = userAddress.houseNumber;
		param.id = userAddress.id;
	});
}

page.on("loaded", function(data) {
	var id = page.getData();
	param.id = id;
	getData(id);
});
//接受地图页面关闭的时候传递回来的数据
var flag = 0;
page.on("result", function(data) {
	if (data.fromPage && data.fromPage == 'selectCommunityAddr') {
    	text_communityAddr.text = data.address;
		param.communityId = data.communityId;
		param.communityAddrId = data.addrId;
	}
});

ui("do_ScrollView_1").redraw();
btnAddr.on("touch", function(data, e) {
	page.hideKeyboard();
	var msg = [];
	if(do_TextField_name.text.trim()==""){
		msg.push("请填写收货人");
	}
	if(do_TextField_phone.text.trim()==""){
		msg.push("请填写手机号");
	}
	if (msg.length > 0) {
		nf.toast(msg.toString());
		return;
	}
	param.contactName = do_TextField_name.text.trim();
	param.contactPhone = do_TextField_phone.text.trim();
	param.houseNumber = do_TextField_houseNumber.text.trim();
	nf.confirm({text:"是否确认提交?",title:"提示",button1text:"提交",button2text:"取消"},function(datacall,e){
		if(datacall == 1){
			qqHttp.post(userUtil.url.address.edit, param, function(data) {
				nf.toast("修改成功");
				app.closePage();
			});
		}
	});
});
//点击背景隐藏输入键盘
root.on("touch",function(){
	page.hideKeyboard();
})